package net.pimpas.web.jsf.page.controller;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import net.pimpas.logging.serviceprovider.LoggingServiceProvider;
import net.pimpas.logging.serviceprovider.LoggingServiceProviderFactoryImpl;
import net.pimpas.operation.exception.OperationException;
import net.pimpas.operation.serviceprovider.OperationServiceProviderFactoryImpl;

public class PageControllerListener implements ServletContextListener {
	
	private static final LoggingServiceProvider logger = 
		LoggingServiceProviderFactoryImpl.getLoggingServiceProvider(PageControllerListener.class);
	private static String PIMPAS_EXTENSION;

	public void contextDestroyed(ServletContextEvent event) {
		PageControllerVisitor.pages.clear();
	}

	public void contextInitialized(ServletContextEvent event) {
		logger.info("Looking for pimpas Extension Parameter ...");
		String parameter = event.getServletContext().getInitParameter("net.pimpas.EXTENSION");
		if(parameter == null) {
			logger.info("Parameter not found... using default!");
			parameter = ".faces";
		}
		PIMPAS_EXTENSION = parameter;
		logger.info("Pimpas Extension configured to ["+parameter+"]");
		try {
			OperationServiceProviderFactoryImpl
			.getOperationServiceProvider()
			.call("ProcessNavigationRules", event, PageControllerVisitor.pages.values());
		} catch (OperationException e) {
			e.printStackTrace();
		}
	}
	
	public static String getExtension( ) {
		return PIMPAS_EXTENSION;
	}
}